﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace TinyCinema.Bussiness.HumanResrouces
{
    public class Configuration : DependencyObject
    {
        public static DependencyProperty IsManagerCreatingProperty = DependencyProperty.Register("IsManagerCreating", typeof(bool), typeof(HumanResrouces.Configuration));
        public bool IsManagerCreating
        {
            get { return (bool)GetValue(IsManagerCreatingProperty); }
            set { SetValue(IsManagerCreatingProperty, value); }
        }
        public static DependencyProperty lstDepartmentProperty = DependencyProperty.Register("lstDepartment", typeof(ObservableCollection<Data.Department>), typeof(HumanResrouces.Configuration));
        public ObservableCollection<Data.Department> lstDepartment
        {
            get { return (ObservableCollection<Data.Department>)GetValue(lstDepartmentProperty); }
            set { SetValue(lstDepartmentProperty, value); }
        }
        public static DependencyProperty lstPositionProperty = DependencyProperty.Register("lstPosition", typeof(ObservableCollection<Data.Position>), typeof(HumanResrouces.Configuration));
        public ObservableCollection<Data.Position> lstPosition
        {
            get { return (ObservableCollection<Data.Position>)GetValue(lstPositionProperty); }
            set { SetValue(lstPositionProperty, value); }
        }
        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(HumanResrouces.Configuration));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        public static DependencyProperty IsCreatingProperty = DependencyProperty.Register("IsCreating", typeof(bool), typeof(Configuration));
        public bool IsCreating
        {
            get { return (bool)GetValue(IsCreatingProperty); }
            set
            { 
                SetValue(IsCreatingProperty, value);
                IsEditable = value;
            }
        }
        public static DependencyProperty FocusingIndexProperty = DependencyProperty.Register("FocusingIndex", typeof(int), typeof(HumanResrouces.Configuration));
        public int FocusingIndex
        {
            get { return (int)GetValue(FocusingIndexProperty); }
            set
            {
                SetValue(FocusingIndexProperty, value);
            }
        }
        public static DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(HumanResrouces.Configuration));
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, value);
            }
        }
        public ICommand CreateCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand SaveDepartmentCommand { get; set; }
        public ICommand SavePositionCommand { get; set; }
        public Messenger Messenger { get; set; }
        public Configuration()
        {
            lstPosition = Data.Position.GetAllPositions();
            lstDepartment = Data.Department.GetAllDepartments();
            Messenger = new Bussiness.Messenger();
            IsEditable = true; // Disable nút Edit
            IsCreating = false;
            CreateCommand = new RelayCommand<Object>(
                (s) => !IsCreating,
                (s) =>
                {
                    IsEditable = true;
                    IsCreating = true;                    
                }
            );
            EditCommand = new RelayCommand<object>(
                (s) => !IsEditable,
                (s) =>
                {
                    if (s == null) return;
                    if (s is Data.Department)
                        (s as Data.Department).Save();
                    else                    
                        (s as Data.Position).Save();
                }
            );
            SaveCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    IsEditable = false;
                    switch (FocusingIndex)
                    {
                        case 0:
                            var temp = new Data.Department();
                            temp.Name = s;
                            temp.Save();
                            lstDepartment.Add(temp);
                            break;
                        case 1:
                            var _temp = new Data.Position();
                            _temp.Name = s;
                            _temp.IsManager = IsManagerCreating;
                            _temp.Save();                            
                            lstPosition.Add(_temp);
                            break;
                    }
                    IsCreating = false;
                }
            ); 
            SaveDepartmentCommand = new RelayCommand<string>(
                 (s) => true,
                 (s) =>
                 {
                    var temp = new Data.Department();
                    temp.Name = s;
                    temp.Save();
                    lstDepartment.Add(temp); 
                 }
             );
            SavePositionCommand = new RelayCommand<string>(
                 (s) => true,
                 (s) =>
                 {
                     var temp = new Data.Position();
                     temp.Name = s;
                     temp.Save();
                     lstPosition.Add(temp);
                 }
             );
            DeleteCommand = new RelayCommand<Object>(
                (s) => true,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result == MessageBoxResult.Yes)
                    {
                        try
                        {
                            if (s is Data.Department)
                            {
                                (s as Data.Department).Delete();
                                lstDepartment.Remove(s as Data.Department);
                            }
                            else
                            {
                                (s as Data.Position).Delete();
                                lstPosition.Remove(s as Data.Position);
                            }
                        }
                        catch
                        {
                            var message2 = new Message.DeleteFailed();
                            Messenger.Send(message2);
                            Data.Execution.Restore();
                        }

                    }
                    
                }
            );
            BackCommand = new RelayCommand<Object>(
                (s) => true,
                (s) =>
                {
                    IsEditable = true;
                    IsCreating = false;
                }
            );
        }
        
    }
}
